package com.web.exiaodao.remote.service.impl;

import com.alibaba.fastjson.JSON;
import com.web.exiaodao.business.constant.JConstant;
import com.web.exiaodao.business.entity.InsHotelRoomsTable;
import com.web.exiaodao.business.repository.InsHotelRoomsTableRepository;
import com.web.exiaodao.remote.service.ApiBannerService;
import com.web.exiaodao.remote.service.ApiCommentService;
import com.web.exiaodao.remote.service.ApiInsHotelRoomsTableService;
import com.web.exiaodao.remote.vo.CommentVo;
import com.web.exiaodao.remote.vo.InsHotelRoomsTableVo;
import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.service.impl.BaseService;
import com.web.webstart.base.util.XaUtil;
import com.web.webstart.base.util.jpa.DynamicSpecifications;
import com.web.webstart.base.util.jpa.SearchFilter;
import com.web.webstart.base.util.jpa.SearchFilter.Operator;
import com.web.webstart.base.util.jpa.WebUitl;
import com.web.webstart.base.util.result.XaResult;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: zhangl
 * @times: 2016-11-04 10:58:15
 * 类的说明：前端APIInsHotelRoomsTable接口实现类
 **/
@Service("ApiInsHotelRoomsTableService")
@Transactional(readOnly = false)
public class ApiInsHotelRoomsTableServiceImpl extends BaseService<InsHotelRoomsTable> implements ApiInsHotelRoomsTableService {

    private static final Logger LOGGER = Logger.getLogger(ApiInsHotelRoomsTableServiceImpl.class);

    @Autowired
    InsHotelRoomsTableRepository insHotelRoomsTableRepository;

    @Autowired
    ApiCommentService commentService;

    @Autowired
    ApiBannerService bannerService;

    @Override
    public XaResult<InsHotelRoomsTableVo> findOne(Long tId) throws BusinessException {
        InsHotelRoomsTable obj = insHotelRoomsTableRepository.findByIdAndStatusNot(tId, XaConstant.Status.delete);
        XaResult<InsHotelRoomsTableVo> xr = new XaResult<>();
        if (XaUtil.isNotEmpty(obj)) {
            xr.setObject(JSON.parseObject(JSON.toJSONString(obj), InsHotelRoomsTableVo.class));
            xr.getObject().setId(obj.getId());

            //查询 前几条 评论
            String sortData2 = "[{property:'createTime',direction:'DESC'}]";
            Pageable pageable2 = WebUitl.buildPageRequest(0, JConstant.COMMENT_TOP_NUM, sortData2);
            Map<String, Object> filterParams2 = WebUitl.getParametersStartingWith(
                    "{'search_EQ_objectId':'"+  tId +"'" +
                            ",'search_EQ_commentTypes':'"+ JConstant.Order_CommentType.INSTIBUTION_VISITING+"'}", "search_");
            XaResult<List<CommentVo>> commentsVo =  commentService.findListNEStatusByFilter(XaConstant.Status.delete, filterParams2,pageable2);
            if (XaUtil.isNotEmpty(commentsVo)) {
                xr.getObject().setHotelRooms_commons(commentsVo.getObject());
            }


            LOGGER.info("查询单个实体----->InsHotelRoomsTable");
        } else {
            xr.error(XaConstant.Message.object_not_find);
            LOGGER.info(XaConstant.Message.object_not_find);
            throw new BusinessException(XaConstant.Message.object_not_find);
        }
        return xr;
    }

    @Override
    public XaResult<InsHotelRoomsTableVo> findLastOne() throws BusinessException {
        XaResult<InsHotelRoomsTableVo> xr = new XaResult<>();
        Map<String, SearchFilter> filters = new HashMap<>();
        filters.put("status", new SearchFilter("status", Operator.NE, XaConstant.Status.delete));
        List<InsHotelRoomsTable> entitys = insHotelRoomsTableRepository.findAll(DynamicSpecifications
                .bySearchFilter(filters.values(), InsHotelRoomsTable.class), new Sort(
                new Sort.Order[]{
                        new Sort.Order(Sort.Direction.DESC, "createTime"),
                        new Sort.Order(Sort.Direction.DESC, "id")
                }));
        if (entitys == null || entitys.size() == 0) {
            xr.error("暂无信息");
        } else {
            xr.setObject(JSON.parseObject(JSON.toJSONString(entitys.get(0)), InsHotelRoomsTableVo.class));
            xr.getObject().setId(entitys.get(0).getId());
            LOGGER.info("查询单个实体----->InsHotelRoomsTable");
        }
        return xr;
    }

    @Override
    public XaResult<List<InsHotelRoomsTableVo>> findListNEStatusByFilter(
            Integer status, Map<String, Object> filterParams, Pageable pageable)
            throws BusinessException {
        Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
        if (status == null) {// 默认显示非删除的所有数据
            status = XaConstant.Status.delete;
        }
        filters.put("status", new SearchFilter("status", Operator.NE, status));
        Page<InsHotelRoomsTable> page = insHotelRoomsTableRepository.findAll(DynamicSpecifications
                .bySearchFilter(filters.values(), InsHotelRoomsTable.class), pageable);
        XaResult<List<InsHotelRoomsTableVo>> xr = new XaResult<List<InsHotelRoomsTableVo>>();
        xr.setObject(JSON.parseArray(JSON.toJSONString(page.getContent()), InsHotelRoomsTableVo.class));
        for (int i = 0; i < page.getContent().size(); i++) {
            xr.getObject().get(i).setId(page.getContent().get(i).getId());
        }
        return xr;
    }

    @Override
    public XaResult<List<InsHotelRoomsTableVo>> findListEQStatusByFilter(
            Integer status, Map<String, Object> filterParams, Pageable pageable)
            throws BusinessException {
        Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
        if (status == null) {// 默认显示正常数据
            status = XaConstant.Status.valid;
        }
        filters.put("status", new SearchFilter("status", Operator.EQ, status));
        Page<InsHotelRoomsTable> page = insHotelRoomsTableRepository.findAll(DynamicSpecifications
                .bySearchFilter(filters.values(), InsHotelRoomsTable.class), pageable);
        XaResult<List<InsHotelRoomsTableVo>> xr = new XaResult<List<InsHotelRoomsTableVo>>();
        xr.setObject(JSON.parseArray(JSON.toJSONString(page.getContent()), InsHotelRoomsTableVo.class));
        for (int i = 0; i < page.getContent().size(); i++) {
            xr.getObject().get(i).setId(page.getContent().get(i).getId());
        }
        return xr;
    }

    @Override
    public XaResult<InsHotelRoomsTableVo> multiOperate(String modelIds,
                                                       Integer status) throws BusinessException {
        XaResult<InsHotelRoomsTableVo> xr = new XaResult<InsHotelRoomsTableVo>();
        if (status == null) {
            status = XaConstant.Status.delete;
        }
        if (modelIds != null) {
            String[] ids = modelIds.split(",");
            for (String id : ids) {
                InsHotelRoomsTable obj = insHotelRoomsTableRepository.findByIdAndStatusNot(Long.parseLong(id), status);
                if (XaUtil.isNotEmpty(obj)) {
                    obj.setStatus(status);
                    obj = insHotelRoomsTableRepository.save(obj);
                    xr.setObject(JSON.parseObject(JSON.toJSONString(obj), InsHotelRoomsTableVo.class));
                    xr.getObject().setId(obj.getId());
                }
            }
        }
        LOGGER.info("更改数据状态----->InsHotelRoomsTable");
        return xr;
    }

    @Override
    public XaResult<InsHotelRoomsTableVo> createModel(InsHotelRoomsTable model)
            throws BusinessException {
        XaResult<InsHotelRoomsTableVo> xr = new XaResult<InsHotelRoomsTableVo>();
        InsHotelRoomsTable obj = insHotelRoomsTableRepository.save(model);
        xr.setObject(JSON.parseObject(JSON.toJSONString(obj), InsHotelRoomsTableVo.class));
        xr.getObject().setId(obj.getId());
        LOGGER.info("新增数据成功----->InsHotelRoomsTable");
        return xr;
    }

    @Override
    public XaResult<InsHotelRoomsTableVo> updateModel(InsHotelRoomsTable model)
            throws BusinessException {
        InsHotelRoomsTable obj = insHotelRoomsTableRepository.findOne(model.getId());
        XaResult<InsHotelRoomsTableVo> xr = new XaResult<InsHotelRoomsTableVo>();
        if (XaUtil.isNotEmpty(obj)) {
            obj.setHotelRooms_Name(XaUtil.isNotEmpty(model.getHotelRooms_Name()) ? model.getHotelRooms_Name() : obj.getHotelRooms_Name());
            obj.setHotelRooms_Price(XaUtil.isNotEmpty(model.getHotelRooms_Price()) ? model.getHotelRooms_Price() : obj.getHotelRooms_Price());
            obj.setHotelRooms_Inventory(XaUtil.isNotEmpty(model.getHotelRooms_Inventory()) ? model.getHotelRooms_Inventory() : obj.getHotelRooms_Inventory());
            obj.setInstitutionId(XaUtil.isNotEmpty(model.getInstitutionId()) ? model.getInstitutionId() : obj.getInstitutionId());
            obj.setHotelRooms_Brief(XaUtil.isNotEmpty(model.getHotelRooms_Brief()) ? model.getHotelRooms_Brief() : obj.getHotelRooms_Brief());
            obj.setHotelRooms_Price_unit(XaUtil.isNotEmpty(model.getHotelRooms_Price_unit()) ? model.getHotelRooms_Price_unit() : obj.getHotelRooms_Price_unit());
            obj.setHourseType(XaUtil.isNotEmpty(model.getHourseType()) ? model.getHourseType() : obj.getHourseType());
            obj.setIsSellByCard(XaUtil.isNotEmpty(model.getIsSellByCard()) ? model.getIsSellByCard() : obj.getIsSellByCard());
            obj.setBookPrice(XaUtil.isNotEmpty(model.getBookPrice()) ? model.getBookPrice() : obj.getBookPrice());
            obj.setBookPrice(XaUtil.isNotEmpty(model.getBookPrice()) ? model.getBookPrice() : obj.getBookPrice());
            obj = insHotelRoomsTableRepository.save(obj);
            xr.setObject(JSON.parseObject(JSON.toJSONString(obj), InsHotelRoomsTableVo.class));
            xr.getObject().setId(obj.getId());
        } else {
            xr.error(XaConstant.Message.object_not_find);
        }
        LOGGER.info("编辑数据成功----->InsHotelRoomsTable");
        return xr;
    }

}
